from sqlalchemy import update as sql_update
from sqlalchemy.future import select

from app.config import db, commit_rollback
from app.model.users import Users
from app.repository.base_repo import BaseRepo


class UsersRepository(BaseRepo):
    model = Users

    @staticmethod
    async def find_by_phone_number(phone_number: str):
        query = select(Users).where(Users.phone_number == phone_number)
        return (await db.execute(query)).scalar_one_or_none()

    @staticmethod
    async def update_password(phone_number: str, password: str):
        query = (
            sql_update(Users)
            .where(Users.phone_number == phone_number)
            .values(password=password)
            .execution_options(synchronize_session="fetch")
        )
        await db.execute(query)
        await commit_rollback()
